package org.ezaero.jmimic.api;

/**
 * Misc. util methods.
 * 
 * @author alex.radeski
 */
public class MimicryUtil {

    /**
     * Infer the method name from the thread stack. This is used to get a mimic name from a JUnit 4.x test method.
     * @param target the class in the call stack, from which get the method name.
     * @return the method name of the first instance of the target class provided.
     */
    public static String inferMimicNameFromStack(Class<?> target) {
        final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement elem : stackTrace) {
            if (elem.getClassName().equals(target.getName())) {
                final String[] tokens = elem.getClassName().split("$");
                return tokens[tokens.length - 1] + "_" + elem.getMethodName();
            }
        }
        throw new IllegalArgumentException("Uable to locate class in stack:" + target);
    }

}
